home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / art&graf.ix / art-0015 / flicker / clips.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  5KB  |  330 lines

  1.  
  2. #include "flicker.h"
  3.  
  4.  
  5. char bap_line_buf[320];    /* byte-a-pixel line buffer */
  6. char fittingc[16];    /* best fit lookup */
  7.  
  8. Cel *clipping = NULL;
  9.  
  10. Cel *
  11. alloc_cel(w, h)
  12. register WORD w, h;
  13. {
  14. register Cel *cel;
  15.  
  16. if ((cel = Alloc_a(Cel)) == NULL)
  17.     return(NULL);
  18. cel->image_size = Raster_block(w, h);
  19. if ( mem_free - cel->image_size - (cel->image_size>>2) < 8000 ||
  20.     (cel->image = (WORD *)alloc(cel->image_size)) == NULL)
  21.     {
  22.     Free_a(cel);
  23.     return(NULL);
  24.     }
  25. cel->width = w;
  26. cel->height = h;
  27. cel->mask = NULL;
  28. return(cel);
  29. }
  30.  
  31. free_cel(cel)
  32. register Cel *cel;
  33. {
  34. if (cel == NULL)
  35.     return;
  36. mfree(cel->image, cel->image_size);
  37. if (cel->mask)
  38.     mfree(cel->mask, cel->image_size>>2);
  39. Free_a(cel);
  40. }
  41.  
  42. mask_cel(cel)
  43. register Cel *cel;
  44. {
  45. if ((cel->mask = (WORD *)alloc(cel->image_size>>2)) == NULL)
  46.     return(0);
  47. calc_mask(cel->image, cel->mask, cel->image_size>>3);
  48. return(1);
  49. }
  50.  
  51. do_paste(x, y)
  52. WORD x, y;
  53. {
  54. if (nozero_flag)
  55.     nozero_celblit(x, y, clipping);
  56. else
  57.     copy_celblit(x, y, clipping);
  58. }
  59.  
  60. rub_paste(x, y)
  61. register WORD x, y;
  62. {
  63. do_paste(x, y);
  64. x += clipping->xoff;
  65. y += clipping->yoff;
  66. draw_frame(white, x, y, x+clipping->width-1, y+clipping->height-1);
  67. }
  68.  
  69. clip_vinit()
  70. {
  71. for (;;)
  72.     {
  73.     check_input();
  74.     see_buffer();
  75.     if (EDN)
  76.         break;
  77.     }
  78. firstx = mouse_x;
  79. firsty = mouse_y;
  80. return(PDN);
  81. }
  82.  
  83.  
  84. paste_clip()
  85. {
  86. register Cel *cel;
  87.  
  88. if ((cel = clipping) == NULL)
  89.     {
  90.     top_line("Nothing to Paste!");
  91.     return;
  92.     }
  93. save_undo();
  94. undo_to_buf();
  95. hide_mouse();
  96. draw_on_buffer();
  97. rub_paste(0, 0);
  98. show_mouse();
  99. see_buffer();
  100. if (clip_vinit())    /* on left button down move it a bit... */
  101.     {
  102.     for (;;)
  103.         {
  104.         if (!PDN)
  105.             break;
  106.         if (mouse_moved)
  107.             {
  108.             hide_mouse();
  109.             unundo();
  110.             rub_paste(mouse_x - firstx, mouse_y - firsty);
  111.             show_mouse();
  112.             see_buffer();
  113.             }
  114.         check_input();
  115.         }
  116.     cel->xoff += mouse_x - firstx;
  117.     cel->yoff += mouse_y - firsty;
  118.     }
  119. hide_mouse();
  120. unundo();
  121. do_paste(0, 0);
  122. show_mouse();
  123. see_buffer();
  124. uninit_some_tools();
  125. }
  126.  
  127. end_vclip()
  128. {
  129. register Cel *cel;
  130.  
  131. free_cel(clipping);
  132. if ((cel = clipping = alloc_cel(x_1 - x_0, y_1 - y_0)) == NULL)
  133.     {
  134.     outta_memory();
  135.     return;
  136.     }
  137. cel->xoff = x_0;
  138. cel->yoff = y_0;
  139. hide_mouse();
  140. unundo();
  141. clip_from_screen(cel, cscreen);
  142. if (!mask_cel(cel))
  143.     {
  144.     outta_memory();
  145.     free_cel(cel);
  146.     clipping = NULL;
  147.     }
  148. show_mouse();
  149. }
  150.  
  151. cut_clip()
  152. {
  153. save_undo();
  154. undo_to_buf();
  155. hide_mouse();
  156. draw_on_buffer();
  157. lines_cursor();
  158. see_buffer();
  159. for (;;)
  160.     {
  161.     check_input();
  162.     if (EDN)
  163.         {
  164.         unundo();
  165.         show_mouse();
  166.         see_buffer();
  167.         break;
  168.         }
  169.     if (mouse_moved)
  170.         {
  171.         unundo();
  172.         lines_cursor();
  173.         see_buffer();
  174.         }
  175.     }
  176. if (RDN)
  177.     {
  178.     uninit_some_tools();
  179.     return;
  180.     }
  181. firstx = mouse_x;
  182. firsty = mouse_y;
  183. for (;;)
  184.     {
  185.     check_input();
  186.     if (!PDN)
  187.         break;
  188.     if (mouse_moved)
  189.         {
  190.         hide_mouse();
  191.         unundo();
  192.         swap_d_box();
  193.         draw_frame(white, x_0, y_0, x_1, y_1);
  194.         show_mouse();
  195.         see_buffer();
  196.         }
  197.     }
  198. x_1++;
  199. y_1++;
  200. end_vclip();
  201. uninit_some_tools();
  202. }
  203.  
  204. find_clip(screen)
  205. WORD *screen;
  206. {
  207. register WORD *c;
  208. register WORD i;
  209. register WORD temp;
  210.  
  211.  
  212. y_0 = 0;
  213. y_1 = YMAX;
  214. c = screen;
  215.  
  216. /* figure out the first line in screen with anything in it*/
  217. i = YMAX;
  218. while (--i >= 0)
  219.     {
  220.     if (!line_zero(c) )
  221.         break;
  222.     c += 80;
  223.     y_0++;
  224.     }
  225. if (y_0 == YMAX)
  226.     {
  227.     return(0);
  228.     }
  229.  
  230. /* figure out the last line in screen with anything in it*/
  231. c = screen + 16000 - 80;    /* c points to start of last line */
  232. i = YMAX;
  233. while (--i >= 0)
  234.     {
  235.     if (!line_zero(c) )
  236.         break;
  237.     c -= 80;
  238.     --y_1;
  239.     }
  240.  
  241. i = y_1 - y_0;        /* the horizontal slice of screen with anything in it */
  242. x_0 = XMAX;
  243. x_1 = 0;
  244. while (--i >= 0)
  245.     {
  246.     temp = first_zeros(c);
  247.     if ((temp) < x_0)
  248.         x_0 = (temp);
  249.     temp = last_zeros(c);
  250.     if ((temp) > x_1)
  251.         x_1 = (temp);
  252.     c -= 80;
  253.     }
  254. return(1);
  255. }
  256.  
  257.  
  258. clip_clip()
  259. {
  260. save_undo();
  261. hide_mouse();
  262. if (!find_clip(cscreen))
  263.     {
  264.     top_line("Picture's Empty!");
  265.     show_mouse();
  266.     return;
  267.     }
  268. draw_frame(white, x_0, y_0, x_1-1, y_1-1);
  269. maybe_zoom();
  270. wait_a_jiffy(12);    /* delay 1/5th second so looks like doing something */
  271. end_vclip();
  272. maybe_zoom();
  273. }
  274.  
  275. pop_cel()    /* show cel momentarily */
  276. {
  277. save_undo();
  278. rub_paste(0, 0);
  279. maybe_zoom();
  280. wait_a_jiffy(20);
  281. see_screen_ix();
  282. }
  283.  
  284.  
  285. cfit_clip()
  286. {
  287. WORD i;
  288. char *image;
  289. WORD line_bytes;
  290. WORD pixels16;
  291. register Cel *cel;
  292.  
  293. if ((cel = clipping) == NULL)
  294.     return;
  295. hide_mouse();
  296. make_fittingc(cel->cmap);
  297. line_bytes = Raster_line(cel->width);
  298. pixels16 = line_bytes>>3;
  299. i = cel->height;
  300. image = (char *)cel->image;
  301. while (--i >= 0)
  302.     {
  303.     conv_buf(image, bap_line_buf, pixels16);
  304.     xbytes(bap_line_buf, fittingc, cel->width);
  305.     iconv_buf(bap_line_buf, image, pixels16);
  306.     image += line_bytes;
  307.     }
  308. show_mouse();
  309. copy_words(sys_cmap, cel->cmap, COLORS);
  310. pop_cel();
  311. }
  312.  
  313. make_fittingc(cmap)
  314. register WORD *cmap;
  315. {
  316. register char *cc;
  317. register WORD i;
  318.  
  319. cc = fittingc;
  320. i = 16;
  321. if (nozero_flag)
  322.     {
  323.     *cc++ = 0;    /* always map color 0 to color 0 */
  324.     cmap++;
  325.     }
  326. while (--i >= 0)
  327.     *cc++ = closest_col(*cmap++, nozero_flag);
  328. }
  329.  
  330.